<?php

namespace app\controller\api;

use app\base\BaseEncryptController;
use app\model\task\TaskModel;
use think\facade\Console;
use think\Request;

/**
 * Task 迎家任务
 * Class Task
 * @package app\controller
 */
class Task extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new TaskModel();
    }

    /**
     * 任务列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getData(Request $request)
    {
        $formData = $request->param();
        $pagenow = $formData['pagenow'];
        $task_type = $formData['task_type'];
        $TaskModel = $this->model;
        //全部
        if ($task_type != "new") {
            $task_type = $task_type == "read" ? 1 : ($task_type == "upload" ? 2 : 3);
            $TaskModel = $TaskModel->where("type", $task_type);
        }

        $data = $TaskModel
            ->where("status", 1)
            ->limit((($pagenow - 1) * 8), $pagenow * 8)
            ->order("id desc")
            ->field("type,price,title,content,label,number,url,exist_number")
            ->select();
        foreach ($data as $key => $value) {
            if ($value['number'] != 0) {
                $data[$key]['bfb'] = $value['exist_number'] / $value['number'] * 100;//已领取人数
            } else {
                $data[$key]['bfb'] = 0;//已领取人数
            }
            $data[$key]['label'] = $data[$key]['label'] != "" ? explode(",", $data[$key]['label']) : [];

        }

        $rsps['code'] = 1;
        $rsps['data'] = $data;
        return self::returnMsg($rsps);
    }

    /**
     * 执行命令
     * @param Request $request
     * @return string
     * @throws \Exception
     */
    public function executeCmd(Request $request)
    {
        $cmd = $request->param('cmd');
        $param = $request->param('param');
        $output = Console::call($cmd, [$param]);
        return $output->fetch();
    }
}
